package com.qinggeng.framework.config.mq.impl;

import com.alibaba.fastjson.JSON;
import com.qinggeng.common.enums.OrdersStatusEnum;
import com.qinggeng.common.enums.PayStatusEnum;
import com.qinggeng.common.exception.CustomException;
import com.qinggeng.framework.config.mq.dto.MessageOrdersDTO;
import com.qinggeng.framework.config.mq.handle.MessageHandle;
import com.qinggeng.project.system.domain.Orders;
import com.qinggeng.project.system.service.IOrdersEvaluateService;
import com.qinggeng.project.system.service.IOrdersService;
import com.qinggeng.project.system.service.MQSendService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Slf4j
@Component
public class OrdersAutoEvaluateHandleImpl implements MessageHandle {

    @Autowired
    private IOrdersService ordersService;

    @Autowired
    private IOrdersEvaluateService ordersEvaluateService;

    @Autowired
    private MQSendService mqSendService;


    @Override
    public void handle(String msg) {

        log.info("订单自动评价处理{}"+msg);
        MessageOrdersDTO ordersDTO= JSON.parseObject(msg,MessageOrdersDTO.class);

        Orders orders=ordersService.selectOrdersByOrdersNo(ordersDTO.getOrdersNo());
        if(null==orders){
            throw new CustomException("订单不存在");
        }
        if(!orders.getPayStatus().equals(PayStatusEnum.已支付.getValue())){
            return;
        }
        if(!orders.getStatus().equals(OrdersStatusEnum.已确认收货.getValue())){
            return;
        }
        //批量评价
        ordersEvaluateService.addEvaluate(orders);
        //订单完结
        mqSendService.sendOrdersEndStatusUpdateAsynMessage(orders.getOrdersNo());

    }
}
